package com.lx.javasunmmrize.SingletonDemo;

/**
 * @Description 懒汉式（线程安全，同步方法）
 * <p>
 * 优点：可以实现懒加载，线程安全，多线程也可以使用
 * 缺点：效率太低，不建议使用
 * @Author lx
 * @Date 2021/2/23 22:12
 **/
public class Singleton04 {

    public static void main(String[] args) {
        Sington04 instanc = Sington04.getInstance();
        Sington04 instanc2 = Sington04.getInstance();
        System.out.println(instanc == instanc2);
        System.out.println(instanc.hashCode());
        System.out.println(instanc2.hashCode());
    }
}

class Sington04 {
    private static Sington04 instance;

    private Sington04() {

    }

    public static synchronized Sington04 getInstance() {
        if (instance == null) {
            instance = new Sington04();
        }
        return instance;
    }

}